/***************************************************************************
 *   Copyright (C) 2008 by Bartosz Dolewski  
 *   E-mail: doles2@gmail.com   
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "piq.h"

Piq::Piq(QWidget *parent, Qt::WFlags f) : QDialog(parent,f)
{
  ui.setupUi(this);

  QTextCodec::setCodecForTr (QTextCodec::codecForName ("UTF-8"));
  setWindowTitle("Piq");
  qDebug()<<"Program has started...\n";

  qDebug()<<"Building connections...\n";
  networkInterface = QNetworkInterface(QNetworkInterface::interfaceFromName("eth0"));
  connect(ui.pushButton1, SIGNAL(clicked()), 	  this, SLOT(showIP()));
  connect(ui.pushButton2, SIGNAL(clicked()), 	  this, SLOT(showMAC()));
  connect(ui.pushButton3, SIGNAL(clicked()),	  this, SLOT(showBroadcast()));
  connect(ui.pushButton4, SIGNAL(clicked()), 	  this, SLOT(showNetmask()));
  connect(ui.pushButton5, SIGNAL(clicked()),	  this, SLOT(refreshEverything()));
  connect(ui.lineEdit5,   SIGNAL(returnPressed()),this, SLOT(setInterfaceName()));
 
  qDebug()<<"\t[+] Connections have finalized....\n";
 //setting tray icon
  icon		   = QIcon (QString(":images/tray_icon.xpm"));
  trayIcon         = new QSystemTrayIcon;
  contextMenu      = new QMenu;
  showHideAction   = new QAction(tr("Show"), this);
  aboutApplication = new QAction(tr("About Application"),this);
  quitApplication  = new QAction(tr("Quit Application"),this);
  isHidden = true;
  trayIcon->setIcon(icon);

  qDebug()<<"Building tray icon....\n";	
  connect(showHideAction,   SIGNAL(triggered()), this, SLOT(showHide()));
  connect(quitApplication,  SIGNAL(triggered()), qApp, SLOT(quit()));
  connect(aboutApplication, SIGNAL(triggered()), this, SLOT(about()));
  
//setting any actions for tray icon
  contextMenu->addAction(showHideAction);
  contextMenu->addSeparator();
  contextMenu->addAction(aboutApplication);
  contextMenu->addSeparator();
  contextMenu->addAction(quitApplication);
  contextMenu->setDefaultAction(showHideAction);
  qDebug()<<"[+] Tray icon has built\n";
  
  connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));

  trayIcon->setContextMenu(contextMenu);
  trayIcon->show();
}

void Piq::showIP()
{
  qDebug()<<"showIP()...\n";
  trayIcon->setToolTip("IP: " + ip);
 if ( networkInterface.isValid() )
    {
        QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
        ip = entry.at(0).ip().toString();
    }
    else
    {
        ip = "unknown";
    }

 if (ui.lineEdit1->text() != ip)
    {
	qDebug()<<"Setting IP adres to lineEdit1...\n";
	ui.lineEdit1->setText(ip);
	qDebug()<<"[+] Done...\n";
    }
}
void Piq::showMAC()
{
 qDebug()<<"showMAC()...\n";
	if(networkInterface.isValid() )
	{
		mac = networkInterface.hardwareAddress();
	}
	else
	{
		mac = "unknown";
	}

	if (ui.lineEdit2->text() != mac)
	{
		ui.lineEdit2->setText(mac);
	}
}

void Piq::showBroadcast()
{
 qDebug()<<"showBroadcast...\n";
	if(networkInterface.isValid())
	{
		//broadcast = networkInterface.broadcast();
		 QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
        	 broadcast = entry.at(0).broadcast().toString();
	}
	else
	{
		broadcast = "unknown";
	}
	if(ui.lineEdit3->text() != broadcast)
	{
		qDebug()<<"Setting up broadcast address...\n";
		ui.lineEdit3->setText(broadcast);
		qDebug()<<"[+] Done...\n";
	}
}
void Piq::showNetmask()
{
 qDebug()<<"showNetmask()...\n";
	if(networkInterface.isValid())
	{
		//netmask = networkInterface.netmask();
		 QList<QNetworkAddressEntry> entry = networkInterface.addressEntries();
        	 netmask = entry.at(0).netmask().toString();
	}
	else
	{
		netmask = "unknown";
	}
	if(ui.lineEdit4->text() != netmask)
	{
		qDebug()<<"Setting up netmask address...\n";
		ui.lineEdit4->setText(netmask);
		qDebug()<<"[+] Done...\n";
	}
}

void Piq::refreshEverything()
{
	qDebug()<<"Refreshing all fields...\n";
	emit showIP();
	emit showMAC();
	emit showBroadcast();
	emit showNetmask();
	qDebug()<<"[+] Done...\n";
}

void Piq::showHide()
{
    if ( isHidden )
    {
        this->show();
        showHideAction->setText( tr("Hide"));
        isHidden = false;
    }

    else
    {
        this->hide();
        showHideAction->setText( tr("Show"));
        isHidden = true;
    }
}

void Piq::trayIconClicked( QSystemTrayIcon::ActivationReason reason )
{
    if ( reason == QSystemTrayIcon::Trigger )
    {
        emit showHide();
    }
}

void Piq::about()
{
 QMessageBox::information(this,tr("About application"),tr("This program was writen by Bartosz Dolewski"));
}

void Piq::setInterfaceName()
{
  qDebug()<<"setInterfaceName()...\n";
  if(ui.lineEdit5->text() != interfaceName)
  {
    qDebug()<<"Setting new interface...\n";
    interfaceName = ui.lineEdit5->text();
    networkInterface = QNetworkInterface::interfaceFromName(interfaceName);
    qDebug()<<"\t[+] Done...\n";
  }
}
